package com.pig4cloud.pig.module.controller.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.wx.UserVoucher;
import com.pig4cloud.pig.module.service.wx.IUserVoucherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户的消费券列Controller
 *
 * @author ruoyi
 * @date 2022-06-07
 */
@Validated
@Api(value = "用户的消费券列控制器", tags = {"用户的消费券列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/user/voucher")
public class UserVoucherController {

    private final IUserVoucherService userVoucherService;

    /**
     * 查询用户的消费券列列表
     */
    @ApiOperation("查询用户的消费券列列表")
    @PreAuthorize("@pms.hasPermission('module:userVoucher:list')")
    @GetMapping("/list")
    public TableDataInfo<UserVoucher> list(@Validated UserVoucher userVoucher) {
        QueryWrapper<UserVoucher> qw = new QueryWrapper<>();

        TableDataInfo<UserVoucher> td = PageUtils.buildDataInfo(userVoucherService.page(PageUtils.buildPage(),qw));
        List<UserVoucher> list = td.getData();
        return td;
    }

    /**
     * 导出用户的消费券列列表
     */
    @ApiOperation("导出用户的消费券列列表")
    @PreAuthorize("@pms.hasPermission('module:userVoucher:export')")
    @SysLog("用户的消费券列")
    @GetMapping("/export")
    public List export(@Validated UserVoucher userVoucher) {
        List<UserVoucher> list = userVoucherService.list();
        // ExcelUtil<UserVoucher> util = new // ExcelUtil<UserVoucher>(UserVoucher.class);
        return list;// util.exportExcel(list, "用户的消费券列");
    }

    /**
     * 获取用户的消费券列详细信息
     */
    @ApiOperation("获取用户的消费券列详细信息")
    @PreAuthorize("@pms.hasPermission('module:userVoucher:query')")
    @GetMapping("/{id}")
    public R<UserVoucher> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        UserVoucher userVoucher = userVoucherService.getById(id);
        return R.ok(userVoucher);
    }

    /**
     * 新增用户的消费券列
     */
    @ApiOperation("新增用户的消费券列")
    @PreAuthorize("@pms.hasPermission('module:userVoucher:add')")
    @SysLog("用户的消费券列")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody UserVoucher userVoucher) {
        return R.okByRow(userVoucherService.save(userVoucher) ? 1 : 0);
    }

    /**
     * 修改用户的消费券列
     */
    @ApiOperation("修改用户的消费券列")
    @PreAuthorize("@pms.hasPermission('module:userVoucher:edit')")
    @SysLog("用户的消费券列")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody UserVoucher userVoucher) {
        return R.okByRow(userVoucherService.updateById(userVoucher) ? 1 : 0);
    }

    /**
     * 删除用户的消费券列
     */
    @ApiOperation("删除用户的消费券列")
    @PreAuthorize("@pms.hasPermission('module:userVoucher:remove')")
    @SysLog("用户的消费券列")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(userVoucherService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
